import ConvertRoutes from './convertRoutes';
//import router from '../../router';
import router from '../../routes';
 
import  dynamicRouter from './dynamicRouter';
/**
 * 动态添加路由
 * author:qlx
 */
const addRoutes = {
  state: {
    rootRoute:[],
    routeParam:[]//这里必须持久化参数,重新生成路由,因为里边的require方法没法持久化
  },
  mutations: {
    add_Routes(state, routeParam) {
      let routeList = [];
      let routeHideList=[];
 
      ConvertRoutes(routeList, routeHideList,routeParam); // 将后台的路由数据components转化成组件

    //   routeList =  [
    //     {
    //     name: 'index',
    //     path: '/index',
    //     component: resolve => require(['../../components/index.vue'], resolve),
    //     meta: {
    //       title: '首页'
    //     }
    //   },
    //   {
    //     name: 'sys_userMange',
    //     path: '/sys_userMange',
    //     component: resolve => require(['../../components/system/user/user.vue'], resolve),
    //     meta: { title: '用户管理' }
    //   } ,
    //   {
    //     name: 'sys_roleMange',
    //     path: '/sys_roleMange',
    //     component: resolve => require(['../../components/system/role/role.vue'], resolve),
    //     meta: { title: '角色管理' }
    //   },
    //   {
    //     name: 'sys_meunMange',
    //     path: '/sys_meunMange',
    //     component: resolve => require(['../../components/system/menu/MenuList.vue'], resolve),
    //     meta: { title: '菜单管理' }
    //   },

    //   {
    //     name: 'sys_deptMange',
    //     path: '/sys_deptMange',
    //     component: resolve => require(['../../components/system/dept/DeptList.vue'], resolve),
    //     meta: { title: '部门管理' }
    //   }
    // ];
 
 

      routeList.push({
        name: 'index',
        path: '/index',
        component: resolve => require(['../../components/index.vue'], resolve),
        meta: {
          title: '首页'
        }
      });



      var rootRoute= [{
        path: '/',
      //  component: resolve => require(['@/components/Common/Home.vue'], resolve),
        component: resolve => require(['../../components/common/Home.vue'], resolve),
        meta: { title: '自述文件' },
        children:routeList
      }];

      //添加新窗口打开的页面
      if(routeHideList&&routeHideList.length>0){
        rootRoute=rootRoute.concat(routeHideList);
      }

      //保存路由参数到store中
      state.routeParam=routeParam;
      state.rootRoute=rootRoute;
      //动态添加路由
      router.addRoutes(rootRoute)
      console.log('输出路由:',JSON.stringify(rootRoute));
    },
    add_Routes_Fresh(state){
       if(state.routeParam&&state.routeParam.length>0){
        this.commit('add_Routes', state.routeParam)
      }
    }
  },
  actions: {
    add_Routes({commit}, routeParam) {
      commit('add_Routes', routeParam)
    },
    add_Routes_Fresh({commit}) {
      commit('add_Routes_Fresh')
    }
  }
}
export default addRoutes